/* PROJECT : http://code.google.com/p/gtk-study-korea/
 *
 * SUB_PROJECT : BJ_QUIZ
 *
 * FILE : main.c
 */

#include <gtk/gtk.h>

#include "headers.h"

/******************************************
 * 상수
 ******************************************/
#define SVN_VERSION  61

/******************************************
 * 변수
 ******************************************/

/******************************************
 * 함수
 ******************************************/
static void destroy( GtkWidget *widget, gpointer   data );
static void on_drawingarea1_expose_event( GtkWidget *widget, gpointer data );
static void on_button3_clicked( GtkWidget *widget, gpointer   data );

/******************************************
 * 입력 :
 * 사용 :
 * 출력 :
 * 설명 :
 *     메인
 ******************************************/
int main( int   argc, char *argv[] )
{
	GtkBuilder *bd_main;

	gchar msg[40];

	// 변수 초기화
	WM.st_id = 0;
	F_LOG = 0;

	F_LOG = (FILE *)(g_fopen("log.txt", "a+") );

	gtk_init (&argc, &argv);

	bd_main = gtk_builder_new();
	if( !gtk_builder_add_from_file( bd_main, "main.glade", NULL) )
	{
		g_fprintf(F_LOG, "\nmain.glade를 열 수 없습니다.");
		fclose(F_LOG);
		exit(1);
	}

	// 초기화 함수
	fn_config_file_init();
	
	// 위젯 생성
	WM.main = GTK_WIDGET( gtk_builder_get_object( bd_main, "main") );
	g_sprintf(msg, "BJ_QUIZ [SVN %d]", SVN_VERSION);
	gtk_window_set_title( GTK_WINDOW(WM.main), msg);

	WM.status = GTK_WIDGET( gtk_builder_get_object( bd_main, "status") );
	WM.st_id = gtk_statusbar_get_context_id( GTK_STATUSBAR(WM.status), "시작");

	WM.drawingarea1 = GTK_WIDGET( gtk_builder_get_object( bd_main, "drawingarea1") );

	WM.button3 = GTK_WIDGET( gtk_builder_get_object( bd_main, "button3") );

	// 위젯 결합

	// 시그널 연결
	gtk_builder_connect_signals( bd_main, NULL);

	g_signal_connect (G_OBJECT (WM.main), "destroy", G_CALLBACK (destroy), NULL);

	g_signal_connect (G_OBJECT (WM.drawingarea1), "expose_event", G_CALLBACK (on_drawingarea1_expose_event), NULL);

	g_signal_connect (G_OBJECT (WM.button3), "clicked", G_CALLBACK (on_button3_clicked), NULL);

	/*  TODO: type some code */

	g_object_unref( bd_main);

	gtk_widget_show_all(WM.main);
	
	gtk_main ();
	
	return 0;
}

/******************************************
 * 입력 :
 * 사용 :
 * 출력 :
 * 설명 :
 *     종료 함수
 ******************************************/
static void destroy( GtkWidget *widget, gpointer   data )
{
	if(F_LOG)
	{
		g_fprintf(F_LOG,"\n종료");
		g_fprintf(F_LOG,"\n=====================");
		fclose(F_LOG);
	}

	gtk_main_quit ();
}

/******************************************
 * 입력 :
 * 사용 :
 * 출력 :
 * 설명 :
 ******************************************/
static void on_drawingarea1_expose_event( GtkWidget *widget, gpointer   data )
{
	//gdk_draw_arc(widget->window,
	//		widget->style->fg_gc[GTK_STATE_NORMAL],
	//		TRUE,
	//		0,0,
	//		widget->allocation.width,
	//		widget->allocation.height,
	//		0, 64 * 360);

	gdk_draw_layout(widget->window,
			widget->style->fg_gc[GTK_STATE_NORMAL],
			5,5,
			gtk_widget_create_pango_layout(widget, "안녕하세요") );
}

/******************************************
 * 입력 :
 * 사용 :
 * 출력 :
 * 설명 :
 *     메뉴에서 설정 선택
 ******************************************/
static void on_button3_clicked( GtkWidget *widget, gpointer   data )
{
	GtkBuilder *bd_conf;

	bd_conf = gtk_builder_new();

	if( !gtk_builder_add_from_file( bd_conf, "config.glade", NULL) )
	{
		g_fprintf(F_LOG, "\nconfig.glade를 열 수 없습니다.");
	}
	else
	{
		fn_config_main(bd_conf);
	}

	g_object_unref( bd_conf);
}

/*******************************************/

